﻿using Prism.Commands;
using Prism.Mvvm;
using Shell.Enums;
using System;
using System.Linq;
using System.Windows;


namespace Shell.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "EAW数据可视化 - Harrrry";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public MainWindowViewModel()
        {

        } 

        private string _HEX;
        public string HEX
        {
            get { return _HEX; }
            set { SetProperty(ref _HEX, value); }
        }

        private string _Number;
        public string Number
        {
            get { return _Number; }
            set { SetProperty(ref _Number, value); }
        }
         

        private DelegateCommand<ConverterType?> _NumberConverterCommand;
        public DelegateCommand<ConverterType?> NumberConverterCommand =>
            _NumberConverterCommand ?? (_NumberConverterCommand = new DelegateCommand<ConverterType?>(ExecuteNumberConverterCommand));

        void ExecuteNumberConverterCommand(ConverterType? type)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(HEX)) throw new Exception("HEX不能为空");
                //先将HEX去除空格转化为二进制 
                var _HEX = string.Join("", HEX.Split(" ").Reverse()); 
                switch (type)
                {
                    case ConverterType.HEXToDoble:
                        //将二进制HEX转化为Doble值
                        Number = BitConverter.ToDouble(BitConverter.GetBytes(Convert.ToInt64(_HEX, 16)), 0).ToString(); 
                        break;
                    case ConverterType.HEXToFloat:
                        //将二进制HEX转化为Float值
                        Number = BitConverter.ToSingle(BitConverter.GetBytes(Convert.ToInt32(_HEX, 16)), 0).ToString(); 
                        break;
                    case ConverterType.HEXToInt32:
                        //将二进制HEX转化为Int32值
                        Number = Convert.ToInt32(_HEX, 16).ToString(); 
                        break;
                    case ConverterType.HEXToInt64:
                        //将二进制HEX转化为Int64值
                        Number = Convert.ToInt64(_HEX, 16).ToString(); 
                        break;  
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private DelegateCommand<ConverterType?> _HEXConverterCommand;
        public DelegateCommand<ConverterType?> HEXConverterCommand =>
            _HEXConverterCommand ?? (_HEXConverterCommand = new DelegateCommand<ConverterType?>(ExecuteHEXConverterCommand));

        void ExecuteHEXConverterCommand(ConverterType? type)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Number)) throw new Exception("Number不能为空");
                switch (type)
                { 
                    case ConverterType.DobleToHEX:
                        //将Doble值转化为二进制HEX
                        HEX = BitConverter.ToString(BitConverter.GetBytes(Convert.ToDouble(Number))).Replace("-", " "); 
                        break;
                    case ConverterType.FloatToHEX:
                        //将Float值转化为二进制HEX
                        HEX = BitConverter.ToString(BitConverter.GetBytes(Convert.ToSingle(Number))).Replace("-", " "); 
                        break;
                    case ConverterType.Int32ToHEX:
                        //将Int32值转化为二进制HEX
                        HEX = Convert.ToInt32(Number).ToString("X8"); // 8位十六进制 
                        break;
                    case ConverterType.Int64ToHEX:
                        //将Int64值转化为二进制HEX 
                        HEX = Convert.ToInt64(Number).ToString("X16"); // 16位十六进制
                        break;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
